** Generating Tables and Plots of
** CCES Confidence Experiment regarding
** Willingness to Attack Iran

*********************
* Stata preliminaries
*********************

clear all
set more off
version 14

set scheme plotplainblind

* change this to fit your directory structure
global path "~/Dropbox/0001 Academic Projects/Ongoing/0148 The Cyber/Posted/FPA Version/Work"
log using "$path/Logs/Figure3Log.log", replace

cd "$path/Data/"
use "CCESUsable.dta"

* labelling and arranging variables

lab var cueCIA "CIA Treatment"
lab var cueAgree "Agreement Treatment"

lab var white "White"
lab var college "College"
lab var female "Female"
lab var threatiran "Perception of Iranian Threat"
lab var dem7 "Democrat"
lab var gop7 "Republican"
lab var ind7 "Independent"
lab var age "Age in Years"

gen interactAgreeCIA = cueAgree * cueCIA
lab var interactAgreeCIA "Agreement x CIA"


gen cueIndependent = .
replace cueIndependent =1 if cueCIA == 0
replace cueIndependent =0 if cueCIA == 1

lab def cueIndependentlab 0 "CIA Endorsement" 1 "Independent Endorsement"
lab val cueIndependent cueIndependentlab

lab def cueAgreelab 0 "Divided" 1 "Support"
lab val cueAgree cueAgreelab


*********************
* Generating figures
*********************

/*
This section conducts a multinomial logistic regression for all respondents
pooled and then for Democrats, Independents, and Republicans (defined by the
7-point scale, with lean-D and lean-R included as Dems and Rs respectively). 
Each regression is followed up by a margins plot producing predicted values
for each treatment condition.
*/

cd "$path/Charts/"

* custom labels for figures
mylabels 0(10)80, myscale(@/100) local(myla)

* Overall
quietly mlogit timetoattack i.cueAgree##i.cueIndependent threatiran ///
	dem7 ind7 age female college white, baseoutcome(2)

	
margins cueAgree, predict(outcome(1))
marginsplot, name(supportAll) title("Overall, Support") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 
	margins cueAgree, predict(outcome(2))
	
marginsplot, name(opposeAll) title("Overall, Oppose") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 
	
margins cueAgree, predict(outcome(3))
marginsplot, name(dontknowAll) ///
	title("Overall, Don't Know") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 

* Democrats
quietly mlogit timetoattack i.cueAgree##i.cueIndependent threatiran ///
	age female college white if dem7 == 1, baseoutcome(2)
	
margins cueAgree, atmeans predict(outcome(1))
marginsplot, name(supportDem) title("Democrats, Support") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 
	
margins cueAgree, atmeans predict(outcome(2))
marginsplot, name(opposeDem) title("Democrats, Oppose") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 
	
margins cueAgree, atmeans predict(outcome(3))
marginsplot, name(dontknowDem) title("Democrats, Don't Know") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 

* Independents
quietly mlogit timetoattack i.cueAgree##i.cueIndependent threatiran ///
	age female college white if ind7 == 1, baseoutcome(2)
	
margins cueAgree, atmeans predict(outcome(1)) 
marginsplot, name(supportInd) title("Independents, Support") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 
	
margins cueAgree, atmeans predict(outcome(2)) 
marginsplot, name(opposeInd) title("Independents, Oppose") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 
	
margins cueAgree, atmeans predict(outcome(3))
marginsplot, name(dontknowInd) title("Independents, Don't Know") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 

* Republicans
quietly mlogit timetoattack i.cueAgree##i.cueIndependent threatiran ///
	age female college white if gop7 == 1, baseoutcome(2)
	
margins cueAgree, atmeans predict(outcome(1))
marginsplot, name(supportGOP) title("Republicans, Support") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 
	
margins cueAgree, atmeans predict(outcome(2))
marginsplot, name(opposeGOP) title("Republicans, Oppose") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 
	
margins cueAgree, atmeans predict(outcome(3))
marginsplot, name(dontknowGOP) title("Republicans, Don't Know") ///
	ytitle("") xtitle("") ylab(`myla') ///
	ysize(3) xsize(2) 

* Pulling together the plots
graph combine supportAll opposeAll dontknowAll supportDem opposeDem dontknowDem ///
	supportInd opposeInd dontknowInd supportGOP opposeGOP dontknowGOP, ///
	title("Figure 3: Predicted Support for Retaliation""by Party and Outcome Variable") ///
	subtitle("2018 CCES Experiment; Multinomial logistic regression",size(small)) ///
	rows(4) xsize(6) ysize(10) ycommon xcommon

graph export "Figure 3.pdf", replace


*********************
* Generating Tables
*********************

* Reworking labels
lab def cueAgreelab2 0 "Divided Endorsement" 1 "Supportive Endorsement"
lab val cueAgree cueAgreelab2

lab def cueCIAlab 0 "Independent Experts" 1 "CIA Endorsement"
lab val cueCIA cueCIAlab

* new mlogits
* Overall
quietly mlogit timetoattack i.cueAgree##i.cueIndependent  threatiran ///
	dem7 ind7 age female college white, baseoutcome(2)
eststo mlog1

* democrats
quietly mlogit timetoattack i.cueAgree##i.cueIndependent   threatiran ///
	age female college white if dem7==1, baseoutcome(2)
eststo mlog2

* independents
quietly mlogit timetoattack i.cueAgree##i.cueIndependent   threatiran ///
	age female college white if ind7==1, baseoutcome(2)
eststo mlog3

* republicans
quietly mlogit timetoattack i.cueAgree##i.cueIndependent   threatiran ///
	age female college white if gop7==1, baseoutcome(2)
eststo mlog4

cd "$path/Tables/"

 
esttab mlog1 mlog2 mlog3 mlog4 ///
	using "CCESExperimentMLogitTablePID7.tex", ///
	label mtitles("Full" "Democrats" "Independents" "Republicans") ///
	interaction(x) nobaselevels noomitted longtable ///
	title("Multinomial Logit Regression of Support for Retaliatory Counter Cyber-Strikes") replace
 
 
* full mlogit coefficient plot
cd "$path/Charts/"

coefplot (mlog1, label(All Respondents)) ///
	(mlog2, label(Democrats) offset(.3)) ///
	(mlog3, label(Independents) offset(.15)) ///
	(mlog4, label(Republicans) offset(-.15)), ///
	drop(_cons dem7 ind7 age threatiran) xline(0) ///
	title("Predictors of Support") ///
	subtitle("Multinomial Coefficients") ///
	nobaselevels
 
 graph export "CCESExperimentMlogitByParty.pdf", replace

 log close
 
